package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lfh
 * @version 1.0
 * @date 2021/5/13 10:37
 */
@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierDao supplierDao;

    @Override
    public Map<String, Object> list(Integer page, Integer rows, String supplierName) {
        HashMap<String, Object> map= new HashMap<>();
            page = page == 0 ? 1 : page;
            int offSet = (page - 1) * rows;
            List<Supplier> suppliers = supplierDao.getSupplierList(offSet,rows,supplierName);
            map.put("total", suppliers.size());
            map.put("rows",suppliers);

        return map;
    }

    @Override
    public ServiceVO save(Supplier supplier) {

        // 判断id为空时是新增，不为空为修改
        if (supplier.getSupplierId() == null){
            supplierDao.save(supplier);
        }else {
            supplierDao.updateSupplier(supplier);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO delete(String ids) {
        if (!StringUtils.isEmpty(ids)){
            String[] split = ids.split(",");
            if (split != null){
                supplierDao.delete(split);
            }
        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
}
